package com.mybatis.mapper;

import com.mybatis.pojo.Flycompanys;
import com.mybatis.pojo.Flysysuser;
import com.mybatis.pojo.FlysysusersCom;
import com.mybatis.pojo.QueryVo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.ResultMap;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Pretty·Boy
 * @date 2018/12/19 20:36
 */
public class MapperTest {
    private SqlSessionFactory sqlSessionFactory;

    @Before
    public void init() throws Exception{
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatis.xml");
        sqlSessionFactory = sqlSessionFactoryBuilder.build(resourceAsStream);
    }

    @Test
    public void myTest() {
        SqlSession sqlSession = sqlSessionFactory.openSession();

        QueryVo queryVo = new QueryVo();
        Flysysuser flysysuser = new Flysysuser();
        flysysuser.setCompanyid("9");
        queryVo.setFlysysuser(flysysuser);
        List ids = new ArrayList<Integer>();
        queryVo.setIds(ids);

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//        Flysysuser user = userMapper.getUserByQueryVo(queryVo);
        List<Flysysuser> users = userMapper.selectUsers(queryVo);
//        System.out.println("user = " + user);
        for (Flysysuser user : users) {
            System.out.println("user = " + user);
        }

        sqlSession.close();
    }


    @Test
    public void  oneToOne() {
        SqlSession sqlSession = sqlSessionFactory.openSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        List<FlysysusersCom> flysysusersComs = mapper.selectOneToOne();

        for (FlysysusersCom flysysusersCom : flysysusersComs) {
            System.out.println(flysysusersCom);
        }
    }


    /**
     * 使用ResultMap  一对一映射
     *
     * @author pretty_boy
     * @time 2018/12/22
     */
    @Test
    public void  findUsersWithResultMap() {
        SqlSession sqlSession = sqlSessionFactory.openSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<Flysysuser> users = mapper.findUsersWithResultMap();
        for (Flysysuser user : users) {
            System.out.println("user = " + user);
        }
    }

    /**
     * 一对多关联映射，查询一个公司对应多个用户
     *
     * @author pretty_boy
     * @time 2018/12/22
     */
    @Test
    public void  findCompanysWithResultMap() {
        SqlSession sqlSession = sqlSessionFactory.openSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<Flycompanys> companys = mapper.findCompanysWithResultMap();
        for (Flycompanys company : companys) {
            System.out.println("company = " + company);
        }
    }


}
